import Vue from "vue";
import confirm from './confirm.vue';

let ConfirmConstructor = Vue.extend(confirm);
let instance;

const Confirm = function(options = {}) {
  return new Promise((resolve,reject) => {
    instance = new ConfirmConstructor({
      el: document.createElement('div'),
      data: options,
    }).$mount();
    document.body.appendChild(instance.$el);
    instance.content = options;
    instance.handleConfirm = function(status) {
      const { btnList } = instance;
      if('close' === status) {
          reject(status);
          instance.isShow = false;
          // instance.closeConfirm();
      }
      if(btnList && btnList.length) {
        btnList.forEach(element => {
          if(['err','error','cancel'].includes(element.funFlag)) {
            reject(element.funFlag)
            instance.isShow = false;
            instance.closeConfirm();
          } else {
            resolve(element.funFlag)
            instance.isShow = false;
            instance.closeConfirm();
          }
        });
      } else {
        if(status === 'cancel') {
          reject(status);
          instance.isShow = false;
          // instance.closeConfirm();
        } else {
          resolve(status)
          instance.isShow = false;
          // instance.closeConfirm();
        }
      }
    }
  })
}

export default Confirm;
